package com.example.warehouse.bus.service.impl;

import com.example.warehouse.bus.dao.persist.repository.IBusSalesRepository;
import com.example.warehouse.bus.pojo.entity.BusSales;
import com.example.warehouse.bus.pojo.param.BusRepertoryReduceGoodsNumParam;
import com.example.warehouse.bus.pojo.param.BusSalesAddNewParam;
import com.example.warehouse.bus.pojo.param.BusSalesSelectParam;
import com.example.warehouse.bus.pojo.param.BusSalesUpdateParam;
import com.example.warehouse.bus.pojo.vo.BusSalesAndSalesbackSelectCNVO;
import com.example.warehouse.bus.pojo.vo.BusSalesAndSalesbackSelectGNVO;
import com.example.warehouse.bus.pojo.vo.BusSalesSelectVO;
import com.example.warehouse.bus.service.IBusRepertoryService;
import com.example.warehouse.bus.service.IBusSalesService;
import com.example.warehouse.common.ex.ServiceException;
import com.example.warehouse.common.pojo.vo.PageBean;
import com.example.warehouse.common.security.CurrentPrincipal;
import com.example.warehouse.common.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * 销售订单_业务层_实现类
 */
@Slf4j
@Service
public class BusSalesServiceImpl implements IBusSalesService {

    @Autowired
    private IBusSalesRepository iBusSalesRepository;

    @Autowired
    private IBusRepertoryService iBusRepertoryService;

    @Override
    public void addNewBusSales(BusSalesAddNewParam busSalesAddNewParam, CurrentPrincipal currentPrincipal) {
        log.debug("开始处理【添加_销售订单】的业务，参数：{},{}", busSalesAddNewParam, currentPrincipal);
        Long idpipo = currentPrincipal.getId();
        int rows = iBusSalesRepository.instert(busSalesAddNewParam, idpipo);
        if (rows != 1) {
            String message = "添加_销售订单失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id, CurrentPrincipal currentPrincipal) {
        log.debug("开始处理【逻辑删除_销售订单】的业务，参数：{},{}", id, currentPrincipal);
        Long idpipo = currentPrincipal.getId();
        int rows = iBusSalesRepository.deleteById(id, idpipo);
        if (rows != 1) {
            String message = "逻辑删除_销售订单失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void update(BusSalesUpdateParam busSalesUpdateParam, CurrentPrincipal currentPrincipal) {
        log.debug("开始处理【任意项修改_销售订单】的业务，参数：{},{}", busSalesUpdateParam, currentPrincipal);
        Long idpipo = currentPrincipal.getId();
        int rows = iBusSalesRepository.update(busSalesUpdateParam, idpipo);
        if (rows != 1) {
            String message = "任意项修改_销售订单失败,服务器忙,请稍后在尝试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void approvalSales(Long id, Integer approval, CurrentPrincipal currentPrincipal, String goodsName, Integer goodsNum) {
        log.debug("开始处理【审批_销售订单】的业务，参数：{},{},{}", id, approval, currentPrincipal);
        Long idpipo = currentPrincipal.getId();
        int rows = iBusSalesRepository.approvalSales(id, approval, idpipo);
        if (rows != 1) {
            String message = "审批_销售订单失败,服务器忙,请稍后在尝试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }

        if (approval == 1) {
            BusRepertoryReduceGoodsNumParam busRepertoryReduceGoodsNumParam = new BusRepertoryReduceGoodsNumParam();
            busRepertoryReduceGoodsNumParam.setGoodsName(goodsName);
            busRepertoryReduceGoodsNumParam.setGoodsNum(goodsNum);
            iBusRepertoryService.reduceGoodsNum(busRepertoryReduceGoodsNumParam);
        }
    }

    @Override
    public PageBean page(BusSalesSelectParam busSalesSelectParam, Integer page, Integer pageSize) {
        log.debug("开始处理【分页模糊查询_销售订单】的业务，参数：{}", busSalesSelectParam);

        Integer start = (page - 1) * pageSize;

        LocalDate begin = busSalesSelectParam.getBegin();

        LocalDate end = busSalesSelectParam.getEnd();

        Long count = iBusSalesRepository.count(busSalesSelectParam, begin, end);

        List<BusSalesSelectVO> list = iBusSalesRepository.list(busSalesSelectParam, start, pageSize, begin, end);

        PageBean pageBean = new PageBean(count, list);
        return pageBean;
    }

    @Override
    public List<BusSalesAndSalesbackSelectCNVO> listCustomerName() {
        log.debug("开始处理【下拉选查询_客户名称】的业务");
        return iBusSalesRepository.listCustomerName();
    }

    @Override
    public List<BusSalesAndSalesbackSelectGNVO> listGoodsName() {
        log.debug("开始处理【下拉选查询_商品名称】的业务");
        return iBusSalesRepository.listGoodsName();
    }

    @Override
    public List<BusSales> selectAll() {
        List<BusSales> list = iBusSalesRepository.selectAll();
        return list;
    }
}
